package Thread;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: homechen
 * Date: 2024-03-16
 * Time: 10:26
 */
public class SolveDeadLockDemo1 {
    public static void main(String[] args) {
        Object lockA = new Object();
        Object lockB = new Object();
        // 线程 一
        Thread t1 = new Thread(() -> {
            System.out.println("线程一等待获取锁A");
            synchronized (lockA) {
                System.out.println("线程一获取到锁A");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程一等待获取锁B");
                synchronized (lockB) {
                    System.out.println("线程一获取到锁B");
                }
            }
        });
        t1.start();
        // 线程 二
        Thread t2 = new Thread(() -> {
            System.out.println("线程二等待获取锁A");
            synchronized (lockA) {
                System.out.println("线程二获取到锁A");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程二等待获取锁B");
                synchronized (lockB) {
                    System.out.println("线程二获取到锁B");
                }
            }
        });
        t2.start();
    }
}
